package com.personalkeeper;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.google.android.maps.GeoPoint;

import android.content.Context;
import android.content.SharedPreferences;
import android.location.Location;

public class PersonalKeeper {

    public static final String ACTION_SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
    public static final String ACTION_SHOW_NOTE = "com.personalkeeper.Action.SHOW_NOTE";
    public static final String ACTION_REQUEST_FOR_LOCATION = "com.personalkeeper.Action.SEND_YOUR_LOCATION";
    public static final String ACTION_START_TRACKING = "com.personalkeeper.Action.START_TRACKING";
    public static final String APP_NAME = "personalkeeper";
    public static final String PACKAGE_NAME = "com.personalkeeper";

    // key-constants for passing arguments through 'Bundles'
    public static final String LOCATION      = PACKAGE_NAME + ".location";
    public static final String LONGITUDE     = PACKAGE_NAME + ".longitude";
    public static final String LATITUDE      = PACKAGE_NAME + ".latitude";
    public static final String DATE          = PACKAGE_NAME + ".date";
    public static final String ID            = PACKAGE_NAME + ".id";
    public static final String TITLE         = PACKAGE_NAME + ".title";
    public static final String DESCRIPTION   = PACKAGE_NAME + ".description";
    public static final String IMAGE_PATH    = PACKAGE_NAME + ".image_path";
    public static final String NAME          = PACKAGE_NAME + ".name";
    public static final String ADDRESS       = PACKAGE_NAME + ".address";
    public static final String REQUEST       = PACKAGE_NAME + ".request";

    public static final String LAST_LOCATION = PACKAGE_NAME + ".lastlocation";


    public static final String NOTE_IMAGE_PREFIX = "IMG_";
    public static final String NOTE_IMAGE_PREVIEW_DIRECTORY_NAME = "preview";


    public static class Settings {
        public static final String PASSIVE_POLL_PERIOD = "passive_poll_period";
        public static final String ACTIVE_POLL_PERIOD = "active_poll_period";
        public static final String IS_TRACKING = "is_tracking";
        public static final String TRACKING_START_TIME = "tracking_start_time";
        public static final String MIN_DISTANCE_BETWEEN_POINTS = "min_distance_between_points";
        public static final String INITIAL_ZOOM_VALUE = "initial_zoom_value"; 

        public static int loadPassivePollPeriod(SharedPreferences pref) {
            return Integer.valueOf(pref.getString(PASSIVE_POLL_PERIOD, ""));
        }
        public static int loadActivePollPeriod(SharedPreferences pref) {
            return Integer.valueOf(pref.getString(ACTIVE_POLL_PERIOD, ""));
        }
        public static float loadMinDistance(SharedPreferences pref) {
            return Float.valueOf(pref.getString(MIN_DISTANCE_BETWEEN_POINTS, ""));
        }
        public static int loadInitialZoomValue(SharedPreferences pref) {
            return Integer.valueOf(pref.getString(INITIAL_ZOOM_VALUE, ""));
        }
    }

    public static class Utils {

        public static File noteImagePathToPreview(File image) {
            return new File(
                       image.getParent() +
                       File.separator +
                       NOTE_IMAGE_PREVIEW_DIRECTORY_NAME +
                       File.separator +
                       image.getName());
        }

        public static Marker createTypicalMarker(Context context, double latitude, double longitude,
                long date, int type) {
            String title = presentableDate(date);
            String snippet = String.format("%f, %f",
                                           latitude,
                                           longitude);

            Marker newMarker = new Marker(
                createGeoPoint(latitude, longitude),
                title,
                snippet,
                type,
                context);

            return newMarker;
        }

        public static Marker createMarkerFromLocation(Context context, Location location, int type) {
            return createTypicalMarker(
                       context,
                       location.getLatitude(),
                       location.getLongitude(),
                       location.getTime(),
                       type);
        }

        public static float distanceBetween(
            double latitude1, double longitude1,
            double latitude2, double longitude2) {
            float[] results = new float[1];
            Location.distanceBetween(latitude1, longitude1, latitude2, longitude2, results);
            return results[0];
        }

        public static float distanceBetween(Location loc1, Location loc2) {
            return distanceBetween(
                       loc1.getLatitude(),
                       loc1.getLongitude(),
                       loc2.getLatitude(),
                       loc2.getLongitude());
        }

        public static GeoPoint createGeoPoint(double latitude, double longitude) {
            return new GeoPoint((int)(latitude * 1E6), (int)(longitude * 1E6));
        }

        public static GeoPoint geoPointFromLocation(Location location) {
            return createGeoPoint(location.getLatitude(), location.getLongitude());
        }

        private static final SimpleDateFormat FORMATTER = new SimpleDateFormat("y.M.d k:m");

        public static String presentableDate(Date date) {
            return FORMATTER.format(date);
        }

        public static String presentableDate(long date) {
            return presentableDate(new Date(date));
        }
    }
}

